package com.pbc.gmp.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.pbc.gmp.commons.shiro.PasswordHash;
import com.pbc.gmp.commons.utils.BeanUtils;
import com.pbc.gmp.commons.utils.PageInfo;
import com.pbc.gmp.commons.utils.StringUtils;
import com.pbc.gmp.dao.OrgMapper;
import com.pbc.gmp.dao.PersonOrgPosMapper;
import com.pbc.gmp.dao.UserRoleMapper;
import com.pbc.gmp.model.UserC;
import com.pbc.gmp.model.UserDetialInfo;
import com.pbc.gmp.model.UserVoNL;
import com.pbc.gmp.model.basemodel.Org;
import com.pbc.gmp.model.basemodel.PersonOrgPos;
import com.pbc.gmp.model.basemodel.User;
import com.pbc.gmp.dao.UserMapper;
import com.pbc.gmp.model.basemodel.UserRole;
import com.pbc.gmp.service.IUserService;

import com.xiaoleilu.hutool.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author kjk
 * @since 2017-04-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource(name="userDao")
    private UserMapper userMapper;
    @Resource(name="userRoleDao")
    private UserRoleMapper userRoleMapper;

    @Resource(name="orgDao")
    private OrgMapper orgMapper;
    @Autowired private PasswordHash passwordHash;
    @Resource(name="personOrgPosDao") private PersonOrgPosMapper personOrgPosMapper;
    @Override
    public List<User> selectUserListByOrgId(Long id) {
        return userMapper.selectUserListByOrgId(id);

    }
    public List<User> selectUserByLoginName(String LoginName){
        return userMapper.selectUserByLoginName(LoginName);
    }

    @Override
    public UserC selectUserInfoDetailByUerId(Long id) {
        return  userMapper.selectUserInfoDetailByUerId(id);
    }

    @Override
    public void updateByVo(UserDetialInfo user) {
        User userVo = BeanUtils.copy(user, User.class);
        //User userInDB=userMapper.selectById(user.getId());
        Org org=orgMapper.selectById(user.getDepartmentid());
        userVo.setMaindepartment(org.getName());
            if (StringUtils.isBlank(userVo.getPassword())) {
                user.setPassword(null);
            }
            this.updateById(userVo);

            Long id = userVo.getId();
            List<UserRole> userRoles = userRoleMapper.selectList(new EntityWrapper<UserRole>().eq("userId",id));
            if (userRoles != null && !userRoles.isEmpty()) {
                for (UserRole userRole : userRoles) {
                    userRoleMapper.deleteById(userRole.getId());
                }
            }
            List<PersonOrgPos> poplist = personOrgPosMapper.selectList(new EntityWrapper<PersonOrgPos>().eq("userId",id));
            if (poplist != null && !poplist.isEmpty()) {
                for (PersonOrgPos pop : poplist) {
                    personOrgPosMapper.deleteById(pop.getId());
                }
            }
            String[] roles = user.getRoles().split(",");
            UserRole userRole = new UserRole();
            for (String string : roles) {
                userRole.setUserid(id);
                userRole.setRoleid(Long.valueOf(string));
                userRole.setId(Long.valueOf("0"));
                userRoleMapper.insert(userRole);
            }
            String[] orgs = user.getOrgs().split(",");

            PersonOrgPos personOrgPos=new PersonOrgPos();
            for (String string : orgs) {
                personOrgPos.setUserid(id);
                personOrgPos.setOrgid(Long.valueOf(string));
                personOrgPos.setPosid(user.getPositionid());
                personOrgPos.setId(Long.valueOf("0"));
                personOrgPosMapper.insert(personOrgPos);
            }


    }
    public Page<User> selectUserPage(Page<User> page) {
        page.setRecords(userMapper.selectUserList(page));
        return page;
    }

    @Override
    public List<UserDetialInfo> selectUserDetialInfoList() {
        return userMapper.selectUserDetialInfoList();
    }

    @Override
    public void selectUserDetialInfoPage(PageInfo pageInfo) {
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
//        page.setOrderByField(pageInfo.getSort());
//        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<Map<String, Object>> list = userMapper.selectUserDetialInfoPage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }



    @Override
    public List<UserDetialInfo> selectUserDetialInfoListByOrgId(Long id) {
        return userMapper.selectUserDetialInfoListByOrgId(id);
    }

    @Override
    public List<UserDetialInfo> selectUserDetialInfoListByDepId(Long id) {
        return userMapper.selectUserDetialInfoListByDepId(id);
    }
    public List<UserDetialInfo> selectUserSimpleInfoListByDepId(Long id) {
        return userMapper.selectUserSimpleInfoListByDepId(id);
    }
    @Override
    public void insertUserVo(UserDetialInfo user) {
        User userVo = BeanUtils.copy(user, User.class);
        //User userInDB=userMapper.selectById(user.getId());
        Org org=orgMapper.selectById(user.getDepartmentid());
        userVo.setMaindepartment(org.getName());
        this.insert(userVo);
        User user1 =userMapper.selectList(new EntityWrapper<User>().eq("loginname",user.getLoginname())).get(0);


        Long id = user1.getId();
        List<UserRole> userRoles = userRoleMapper.selectList(new EntityWrapper<UserRole>().eq("userId",id));
        if (userRoles != null && !userRoles.isEmpty()) {
            for (UserRole userRole : userRoles) {
                userRoleMapper.deleteById(userRole.getId());
            }
        }
        List<PersonOrgPos> poplist = personOrgPosMapper.selectList(new EntityWrapper<PersonOrgPos>().eq("userId",id));
        if (poplist != null && !poplist.isEmpty()) {
            for (PersonOrgPos pop : poplist) {
                personOrgPosMapper.deleteById(pop.getId());
            }
        }
        String[] roles = user.getRoles().split(",");
        UserRole userRole = new UserRole();
        for (String string : roles) {
            userRole.setUserid(id);
            userRole.setRoleid(Long.valueOf(string));
            userRole.setId(Long.valueOf("0"));
            userRoleMapper.insert(userRole);
        }
        String[] orgs = user.getOrgs().split(",");

        PersonOrgPos personOrgPos=new PersonOrgPos();
        for (String string : orgs) {
            personOrgPos.setUserid(id);
            personOrgPos.setOrgid(Long.valueOf(string));
            personOrgPos.setPosid(user.getPositionid());
            personOrgPos.setId(Long.valueOf("0"));
            personOrgPosMapper.insert(personOrgPos);
        }

    }
//    public void insertUserVoRe(UserDetialInfo user)
//    {
//
//
//    }
    public void deleteBatchByIds(String ids){
        String[] idlist =ids.split(",");

        for (String id : idlist) {
            this.deleteById(Convert.toLong(id));
            userRoleMapper.delete(new EntityWrapper<UserRole>().eq("userId",Convert.toLong(id)));
            personOrgPosMapper.delete(new EntityWrapper<PersonOrgPos>().eq("userId",Convert.toLong(id)));
        }


    }
    public void updateEnableUserByIds(String ids ){
        String[] idlist =ids.split(",");

        for (String id : idlist) {
            userMapper.updateEnableByUserId(Convert.toLong(id));
        }
    }
    public void updateUnableUserByIds(String ids ){
        String[] idlist =ids.split(",");

        for (String id : idlist) {
            userMapper.updateUnableByUserId(Convert.toLong(id));
        }
    }

    @Override
    public void updateUserDefaultPwdById(Long id) {
        User user =userMapper.selectById(id);
        user.setPassword(passwordHash.toHex("password",user.getSalt()));

        userMapper.updateById(user);


    }
}
